package controller

import (
	"gitee.com/molonglove/goboot/gin"
	"github.com/docker/docker/api/types"
	"helper-api/app/constant"
	"helper-api/app/models/request"
	"helper-api/app/models/response"
	"helper-api/app/service"
	"net/http"
	"strconv"
)

var Docker = new(DockerController)

type DockerController struct{}

/** ===================================== docker container 相关接口 ============================================================= */

// ContainerList 容器列表
func (d *DockerController) ContainerList(c *gin.Context) {
	var (
		result    *response.PageData
		param     request.DCListRequest
		customErr *response.BusinessError
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if result, customErr = service.Docker.ContainerList(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ContainerOperation 容器操作
func (d *DockerController) ContainerOperation(c *gin.Context) {
	var (
		param     request.DCOperateRequest
		customErr *response.BusinessError
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if customErr = service.Docker.ContainerOperate(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(response.Success))
}

// ContainerDetail 查看容器详情
func (d *DockerController) ContainerDetail(c *gin.Context) {
	var (
		customErr *response.BusinessError
		result    any
		id        string
		ok        bool
	)
	if id, ok = c.GetQuery("id"); !ok {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if result, customErr = service.Docker.ContainerInspect(id); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ContainerLogs 获取容器日志
func (d *DockerController) ContainerLogs(c *gin.Context) {
	var (
		param     request.DCLogQueryRequest
		customErr *response.BusinessError
		err       error
		logs      string
	)
	if err = c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if logs, customErr = service.Docker.ContainerLogs(param); err != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(logs))
}

/** ===================================== docker images 相关接口 ============================================================= */

// ImageList 镜像列表
func (d *DockerController) ImageList(c *gin.Context) {
	var (
		result    *response.PageData
		param     request.DIListRequest
		customErr *response.BusinessError
		err       error
	)
	c.DefaultPostForm("order", "true")
	c.DefaultPostForm("isAll", "true")
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if result, customErr = service.Docker.ImageList(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ImageSearch 镜像搜索
func (d *DockerController) ImageSearch(c *gin.Context) {
	var (
		result    []response.DISearchResponse
		param     request.DISearchRequest
		customErr *response.BusinessError
		err       error
	)
	c.DefaultPostForm("isOfficial", "-1")
	c.DefaultPostForm("isAutomated", "-1")
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if result, customErr = service.Docker.ImageSearch(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ImagePull 镜像拉取
func (d *DockerController) ImagePull(c *gin.Context) {
	var (
		result    *response.DIPullResultResponse
		customErr *response.BusinessError
		userId    int64
		name      string
		ok        bool
	)
	if name, ok = c.GetQuery("name"); !ok {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	userId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Docker.ImagePull(name, userId); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ImagePullResult 获取镜像拉取结果
func (d *DockerController) ImagePullResult(c *gin.Context) {
	var (
		id        int64
		idStr     string
		ok        bool
		err       error
		result    response.DIPullResultResponse
		customErr *response.BusinessError
	)
	if idStr, ok = c.GetQuery("id"); !ok {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if id, err = strconv.ParseInt(idStr, 10, 64); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if result, customErr = service.Docker.ImagePullResult(id); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ImageTags 获取镜像标签
func (d *DockerController) ImageTags(c *gin.Context) {
	var (
		result    *response.PageData
		customErr *response.BusinessError
		param     request.DIImageTagRequest
		err       error
	)
	if err = c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if result, customErr = service.Docker.ImageTags(param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ImageDelete 镜像删除
func (d *DockerController) ImageDelete(c *gin.Context) {
	var (
		ok        bool
		imageId   string
		isForce   bool
		customErr *response.BusinessError
	)
	c.DefaultQuery("isForce", "false")
	isForce = c.GetBool("isForce")
	if imageId, ok = c.GetQuery("id"); !ok {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if customErr = service.Docker.ImageRemove(imageId, isForce); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok("删除镜像成功"))
}

// ImageSetTag 镜像设置Tag
func (d *DockerController) ImageSetTag(c *gin.Context) {
	var (
		customErr *response.BusinessError
		param     request.DISetTagRequest
		err       error
	)
	if err = c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if customErr = service.Docker.ImageSetTag(param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok("操作成功"))
}

// ImageInfo 获取镜像详情
func (d *DockerController) ImageInfo(c *gin.Context) {
	var (
		customErr *response.BusinessError
		result    *types.ImageInspect
		imageId   string
		ok        bool
	)
	if imageId, ok = c.GetQuery("imageId"); !ok {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if result, customErr = service.Docker.ImagesInfo(imageId); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ImageExport 镜像导出
func (d *DockerController) ImageExport(c *gin.Context) {
	var (
		result    *response.DIPullResultResponse
		customErr *response.BusinessError
		param     request.DIExportRequest
		err       error
	)
	if err = c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Docker.ImageExport(param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ImageImport 镜像导入
func (d *DockerController) ImageImport(c *gin.Context) {

}

// ImageBuild 镜像构建
func (d *DockerController) ImageBuild(c *gin.Context) {

}

// ImageClean 镜像清理
func (d *DockerController) ImageClean(c *gin.Context) {

}

/** ===================================== docker volumes 相关接口 ============================================================= */

/** ===================================== docker network 相关接口 ============================================================= */
